﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Device;
using BDPAutodetect.Device;
using BDPAutodetect.Hardware;
using BDPAutodetect.Models;

namespace BDPAutodetect.Simcore
{
    /// <summary>
    /// DIDO输入输出板设备仿真
    /// </summary>
    public class DeviceSimulatorSensorSign : DeviceSimulatorModbus
    {
        /// <summary>
        /// 设备仿真数据
        /// </summary>
        protected Dictionary<DeviceModule, DeviceSimulatorDataSensorSign> DeviceSimulatorDataDict { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argDevice">设备连接对象</param>
        public DeviceSimulatorSensorSign(DeviceSerialModbus<IDevicePara> argDevice)
            : base($"DIDO输入输出板仿真设备", DeviceType.SensorSign, DeviceModule.None, argDevice)
        {
            this.DeviceSimulatorDataDict = new Dictionary<DeviceModule, DeviceSimulatorDataSensorSign>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            for (DeviceModule mod = DeviceModule.One; mod <= DeviceModule.Eight; mod++)
            {
                this.DeviceSimulatorDataDict.Add(mod, new DeviceSimulatorDataSensorSign(mod));
            }
        }

        /// <summary>
        /// 启动/关闭开关
        /// </summary>
        public void StartupSwitch(DeviceModule argDeviceModule, SwitchType argSwitchType)
        {
            var simulatorData = this.DeviceSimulatorDataDict[argDeviceModule];
            if (simulatorData == null) return;
            switch (argSwitchType)
            {
                case SwitchType.On:
                    simulatorData.SwitchOff = false;
                    break;
                case SwitchType.Off:
                    simulatorData.SwitchOff = true;
                    break;
            }
        }

        /// <summary>
        /// 接收设备数据解析
        /// </summary>
        /// <returns></returns>
        protected override DeviceCommandData? DeviceDataParse(ModbusSlaveData<StructInputShort> argModbusData, string argModbusDataHex)
        {
            var commandData = base.DeviceDataParse(argModbusData, argModbusDataHex);
            if (commandData == null) return commandData;
            //检测是否存在字典
            if (!this.DeviceSimulatorDataDict.ContainsKey(commandData.DeviceModule)) return commandData;
            //获取仿真数据实例
            var simulatorData = this.DeviceSimulatorDataDict[commandData.DeviceModule];
            switch (commandData.DeviceTargetType)
            {
                case DeviceTargetType.SensorSign_LedLightRedWrite:
                    simulatorData.LedLightRed = DeviceHelper.Instance.GetSwitchType(argModbusData.SlaveData.RegisterData);
                    break;
                case DeviceTargetType.SensorSign_LedLightGreenWrite:
                    simulatorData.LedLightGreen = DeviceHelper.Instance.GetSwitchType(argModbusData.SlaveData.RegisterData);
                    break;
                case DeviceTargetType.SensorSign_LedLightYellowWrite:
                    simulatorData.LedLightYellow = DeviceHelper.Instance.GetSwitchType(argModbusData.SlaveData.RegisterData);
                    break;
                case DeviceTargetType.SensorSign_HummerWrite:
                    simulatorData.Hummer = DeviceHelper.Instance.GetSwitchType(argModbusData.SlaveData.RegisterData);
                    break;
            }
            return commandData;
        }

        /// <summary>
        /// 设备数据响应解析
        /// </summary>
        /// <param name="argCommandData"></param>
        protected override byte[] DeviceDataResponseParse(DeviceCommandData argCommandData)
        {
            List<byte> responseBytes = new List<byte>();
            //获取响应指令
            var CommandBytes = base.DeviceDataResponseParse(argCommandData);
            responseBytes.AddRange(CommandBytes);
            //获取数据
            if (argCommandData.DeviceTargetType == DeviceTargetType.SensorSign_SwitchRead)
            {
                bool[] switchBits = this.DeviceSimulatorDataDict.Values.Select(v => v.SwitchOff).ToArray();
                byte switchByte = switchBits.ToArrayByte();
                responseBytes.Add(1);
                responseBytes.Add(switchByte);
            }
            else
            {
                ushort nData = 0;
                var simulatorData = this.DeviceSimulatorDataDict[argCommandData.DeviceModule];
                switch (argCommandData.DeviceTargetType)
                {
                    case DeviceTargetType.SensorSign_LedLightRedWrite: nData = DeviceHelper.Instance.GetSwitchData(simulatorData.LedLightRed); break;
                    case DeviceTargetType.SensorSign_LedLightGreenWrite: nData = DeviceHelper.Instance.GetSwitchData(simulatorData.LedLightGreen); break;
                    case DeviceTargetType.SensorSign_LedLightYellowWrite: nData = DeviceHelper.Instance.GetSwitchData(simulatorData.LedLightYellow); break;
                    case DeviceTargetType.SensorSign_HummerWrite: nData = DeviceHelper.Instance.GetSwitchData(simulatorData.Hummer); break;
                }
                responseBytes.Add(2);
                responseBytes.AddRange(nData.StructToBytes());
            }
            //计算CRC
            var crcBytes = responseBytes.ToArray().ToCheckCRCModbus();
            responseBytes.AddRange(crcBytes);
            return responseBytes.ToArray();
        }
    }

    /// <summary>
    /// 仿真数据
    /// </summary>
    public class DeviceSimulatorDataSensorSign
    {
        /// <summary>
        /// 设备指标模组
        /// </summary>
        public DeviceModule Module { get; set; }
        /// <summary>
        /// LED红灯（true:开, false:关）
        /// </summary>
        public SwitchType LedLightRed { get; set; }
        /// <summary>
        /// LED绿灯（true:开, false:关）
        /// </summary>
        public SwitchType LedLightGreen { get; set; }
        /// <summary>
        /// LED黄灯（true:开, false:关）
        /// </summary>
        public SwitchType LedLightYellow { get; set; }
        /// <summary>
        ///  蜂鸣器（true:开, false:关）
        /// </summary>
        public SwitchType Hummer { get; set; }
        /// <summary>
        /// 关闭开关量（true:关, false:开）
        /// </summary>
        public bool SwitchOff { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DeviceSimulatorDataSensorSign(DeviceModule argModule)
        {
            this.Module = argModule;
            this.SwitchOff = true;
        }
    }
}
